<%@ page import="org.json.simple.*" %> <%--json_simple_1-1.jar--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    import = "java.sql.*"
	%>
<%
	request.setCharacterEncoding("UTF-8");
	
	String url = "jdbc:oracle:thin:@155.230.193.42:1521:oraknu";
	String user = "system";
	String pass = "DBLab012345";
	Connection con = null;
	String sql = null;
	int result;
	JSONObject resultJson = new JSONObject();
	JSONArray resultArray = new JSONArray();

	final int maxMemoCount = 10;
	int pageNumber = 1;

	String obj = request.getParameter("page");
	if( !(obj == null  || obj.trim().length() == 0))
		pageNumber = Integer.parseInt(obj);
	
	String id = (String) session.getAttribute("logID");
	try
	{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		System.out.println("success");
	}
	catch(ClassNotFoundException e)
	{
		System.err.println("error = " + e.getMessage());
		System.exit(1);
	}
	try
	{
		con = DriverManager.getConnection(url, user, pass);
	}
	catch(SQLException e)
	{
		System.err.println("sql error = " + e.getMessage());
		System.exit(1);
	}
	try
	{
		con.setAutoCommit(false);
		
		sql = "select mid, to_char(mtime, 'yyyy:mm:dd hh:mi:ss') as time, mtext, rnum " +
			 " from (select memo.*, rownum as rnum from MEMO where id_u = ? order by mtime desc) m where rnum > ? and rnum <= ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, id);
		pstmt.setInt(2, (pageNumber - 1) * maxMemoCount);
		pstmt.setInt(3, (pageNumber) * maxMemoCount);

		ResultSet rs = pstmt.executeQuery();
		while(rs.next())
		{
			resultJson = new JSONObject();
			resultJson.put("mid", rs.getString("mid"));
			resultJson.put("time", rs.getString("time"));
			resultJson.put("text", rs.getString("mtext"));
			
			resultArray.add(resultJson);
		}
		pstmt.close();
		con.close();
		
		out.print(resultArray);
	}
	catch(Exception e)
	{
		System.err.println("sql error = " + e.getMessage());
	}
			
%>